{-----------------------------------------------------------------
 
  (c) 2008-2009 Markus Dittrich 
 
  This program is free software; you can redistribute it 
  and/or modify it under the terms of the GNU General Public 
  License Version 3 as published by the Free Software Foundation. 
 
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License Version 3 for more details.
 
  You should have received a copy of the GNU General Public 
  License along with this program; if not, write to the Free 
  Software Foundation, Inc., 59 Temple Place - Suite 330, 
  Boston, MA 02111-1307, USA.

--------------------------------------------------------------------}

-- | PrettyPrint provides tools for colored output to the terminal
module Helpers.PrettyPrint ( putColorStr,
                            putColorStrLn,
                            putColorBStr,
                            putColorBStrLn,
                            Color(..)
                          ) where


-- imports
import qualified Data.ByteString as B(ByteString, concat, putStr)
import Prelude


-- local imports
import Helpers.ByteString


-- | available colors
data Color = Black | Red | Green | Yellow | Blue | Magenta 
           | Cyan | White | Reset 
           deriving(Enum)



-- | available intensities
data Intensity = Normal | Bold
               deriving(Eq)



-- | convert a color into the corresponding color code string
get_color_code :: Color -> String
get_color_code = show . fromEnum 



-- | convert an intensity to the corresponding color code string
get_intensity_code :: Intensity -> String
get_intensity_code x
  | x == Normal  = "22"
  | x == Bold    = "1"
  | otherwise    = "1"



-- | generate color string header
gen_color_header :: Intensity -> Color -> String
gen_color_header intensity col = 
    "\ESC[" 
    ++ (get_intensity_code intensity) 
    ++ ";3" 
    ++ (get_color_code col) 
    ++ "m"



-- | generate final terminal sequence
gen_color_footer :: String
gen_color_footer = "\ESC[0;m"



-- | print a colored string to the terminal
putColorStr :: Color -> String -> IO ()
putColorStr color text = 
  (putStr $ gen_color_header Bold color)
  >> putStr text
  >> (putStr $ gen_color_footer) 


putColorStrLn :: Color -> String -> IO ()
putColorStrLn color text = putColorStr color (text ++ "\n")



-- | print a colored ByteString to the terminal
putColorBStr :: Color -> B.ByteString -> IO ()
putColorBStr color text = 
  (putStr $ gen_color_header Bold color)
  >> B.putStr text
  >> (putStr $ gen_color_footer)


putColorBStrLn :: Color -> B.ByteString -> IO ()
putColorBStrLn color text = 
  putColorBStr color (B.concat [text,newline])
